﻿/// <reference path="backbone.js" />
/// <reference path="jquery.js" />
/// <reference path="json2.js" />
/// <reference path="underscore.js" />




var ggc = { luck28: {} };
$(function () {
    //号码模型
    Number = Backbone.Model.extend({
        defauts: {
            check: false,
            value: 0,//值
            currentodds: 0,//当前赔率
            lastodds: 0//上期赔率
        },
        //点击方法
        checked: function () {
            this.save({ check: !this.get("check") })
        }
    })


    //号码集合
    NumberList = Backbone.Collection.extend({
        model: Number,
        initialize: function () {
            //var that = this;
            //$.ajax({
            //    type: "Post",
            //    url: "",
            //    data: "XX=xx@xxx=xxx",
            //    success: function (data) {
            //        that.model = data;//取到list集合
            //    }
            //})
            this.add([
              { check: false, value: 1, currentodds: 2, lastodds: 3 },
              { check: false, value: 11, currentodds: 22, lastodds: 33 }
            ]);
        },
        //选出所有勾中的
        selectchecked: function () {
            return this.filter(function (number) { return number.get('check'); });
        }
    })


    ggc.luck28.numberlist = new NumberList;

    NumberView = Backbone.View.extend({
        tagName: "tr",//这是个tr的视图
        template: $("#item-number").template(),
        evrnts: {
            "click .span1": "changevalue",
            "click input[type=checkbox]": "checked"
        },
        initialize: function () {
            this.model.bind('change', this.render);
        },
        //视图重绘
        render: function () {
            var element = jQuery.tmpl(this.template, this.model.toJSON());
            $(this.el).html(element);
            return this;
        },

        //翻倍
        changevalue: function (e) {
            var val = $(this.el).find("input_line").val();
            var multiple = $(e.currentTarget).html();
            this.model.save({ "value": Math.round(val * multiple) });
        },

        checked: function () {
            this.model.checked();
        }

    })
    //集合视图
    NumberListView = Backbone.View.extend({
        el: $("#tablenumberlist"),
        initialize: function () {


        },
        render: function () {
            //这里循环加载Number视图
            for (i = 0; i < ggc.luck28.numberlist.length; i++) {
                var view = new NumberView({ model: ggc.luck28.numberlist.models[i] });
                $(this.el).append(view.render().el);
            }
        }
    })
    ggc.luck28.numberlistview = new NumberListView;
    ggc.luck28.numberlistview.render();
})